package edu.kit.aifb.ldstream.learner.tasks.mod;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import edu.kit.aifb.ldstream.env.Environment;

public class ValueCache {

	private File valueCache = new File(Environment.VALUE_CACHE);
	private BufferedWriter writer;
	private BufferedReader reader;
	
	public ValueCache() {
		init();
		try {
			this.writer = new BufferedWriter(new FileWriter(this.valueCache), 1024 * 1024);
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			this.reader = new BufferedReader(new FileReader(this.valueCache), 1024 * 1024);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public void write(String value){
		try {
			this.writer.write(value);
			this.writer.write("\n");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String next(){
		String value = null;
		try {
			value = this.reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return value;
	}
	
	private void init(){
		if(this.valueCache.exists()){
			this.valueCache.delete();
		}
		try {
			this.valueCache.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.valueCache.deleteOnExit();
	}
	
	public void closeWriter(){
		try {
			this.writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void closeReader(){
		try {
			this.reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
